package cn.yuanqiao.web.controller.archive.storeRoom;

import cn.yuanqiao.archive.storeRoom.domain.Storeroom;
import cn.yuanqiao.archive.storeRoom.service.IStoreroomService;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 库房信息Controller
 * 
 * @author xy
 * @date 2023-06-16
 */
@RestController
@RequestMapping("/archive/store")
public class StoreroomController extends BaseController
{
    @Autowired
    private IStoreroomService storeroomService;

    /**
     * 查询库房信息列表
     */

    @PreAuthorize("@ss.hasPermi('archive:warehouseManagement:warehouseInformation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Storeroom storeroom)
    {
        startPage();
        List<Storeroom> list = storeroomService.selectStoreroomList(storeroom);
        return getDataTable(list);
    }

    /**
     * 导出库房信息列表
     */


    /*@PreAuthorize("@ss.hasPermi('storeroom:store:export')")*/
    @Log(title = "库房信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Storeroom storeroom)
    {
        List<Storeroom> list = storeroomService.selectStoreroomList(storeroom);
        ExcelUtil<Storeroom> util = new ExcelUtil<Storeroom>(Storeroom.class);
        util.exportExcel(response, list, "库房信息数据");
    }

    /**
     * 获取库房信息详细信息
     */


    @PreAuthorize("@ss.hasPermi('archive:warehouseManagement:warehouseInformation:store:detail')")
    @GetMapping(value = "/{storeId}")
    public AjaxResult getInfo(@PathVariable("storeId") Long storeId)
    {
        return success(storeroomService.selectStoreroomByStoreId(storeId));
    }

    /**
     * 新增库房信息
     */

/*    @PreAuthorize("@ss.hasPermi('storeroom:store:add')")*/
    @PreAuthorize("@ss.hasPermi('archive:warehouseManagement:warehouseInformation:store:add')")
    @Log(title = "库房信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Storeroom storeroom)
    {
        Map map=storeroomService.insertStoreroom(storeroom);
        if(map.get("code").toString().equals("200")){
           return AjaxResult.success();
        }else{
           return AjaxResult.error(map.get("msg").toString());
        }

    }

    /**
     * 修改库房信息
     */

  /*  @PreAuthorize("@ss.hasPermi('storeroom:store:edit')")*/
    @PreAuthorize("@ss.hasPermi('archive:warehouseManagement:warehouseInformation:store:edit')")
    @Log(title = "库房信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Storeroom storeroom)
    {
        return toAjax(storeroomService.updateStoreroom(storeroom));
    }

    /**
     * 删除库房信息
     */

  /*  @PreAuthorize("@ss.hasPermi('storeroom:store:remove')")*/
    @PreAuthorize("@ss.hasPermi('archive:warehouseManagement:warehouseInformation:store:delete')")
    @Log(title = "库房信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{storeIds}")
    public AjaxResult remove(@PathVariable Long[] storeIds)
    {
        return toAjax(storeroomService.deleteStoreroomByStoreIds(storeIds));
    }

    /**
     * 通过库房ID获取区域
     */
    /*@GetMapping(value = "/getRegion/{storeId}")*/
    public AjaxResult getRegion(@PathVariable("storeId") Long storeId){
        return AjaxResult.success(storeroomService.selectStoreroomByStoreId(storeId).getRegion());
     }
     //验证是否下面有柜子
    /**
     * 验证该是否可进行修改
     */
    @GetMapping(value = "/checkStore/{storeId}")
    public AjaxResult checkShelf(@PathVariable("storeId") Long storeId)
    {
        int num=storeroomService.checkStore(storeId);
        if(num>0){
            return AjaxResult.success(500);
        }else{
            return AjaxResult.success(200) ;
        }
    }


}
